package Servlet;

import static org.junit.Assert.assertNotNull;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.catalina.User;
import org.eclipse.jdt.internal.compiler.parser.RecoveredRequiresStatement;

//import com.mysql.cj.Session;

import DAO.Dao;
import DAO.Dim;
import DAO.UserDao;
import ImplementEntity.ImplementJdbcEntity;

/**
 * 	实锟斤拷锟斤拷MVC锟斤拷目锟叫碉拷C---Servlet 
 * Servlet implementation class AllServerlet
 */
@WebServlet("*.do")
public class doServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	//全锟街憋拷锟斤拷,锟斤拷锟斤拷全锟街碉拷锟斤拷
	Cookie cookie=new Cookie("users",null);
	private UserDao userDaoJdbcEntity= new ImplementJdbcEntity(); 
	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public doServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
		
		request.setCharacterEncoding("utf-8");response.setContentType("text/html; charset=UTF-8");//锟斤拷锟斤拷锟斤拷锟斤拷,锟斤拷止锟斤拷锟斤拷
		
		/**
		 * 	锟斤拷锟矫凤拷锟斤拷锟斤拷锟� and 锟斤拷锟斤拷URL锟斤拷锟轿ｏ拷锟斤拷锟斤拷
		 */
		String servletPath = request.getServletPath();//锟斤拷锟铰凤拷锟�
		String methedString = servletPath.substring(1);//去锟斤拷/
		methedString = methedString.substring(0, methedString.length() - 3);//去锟斤拷.do
		try {
			/**
			 * 	锟斤拷取锟斤拷锟斤拷锟叫碉拷锟斤拷锟叫凤拷锟斤拷
			 */
			Method method = getClass().getDeclaredMethod(methedString, HttpServletRequest.class,HttpServletResponse.class);
			
			/**
			 * 	实锟斤拷实锟斤拷锟斤拷锟斤拷锟斤拷&锟斤拷锟斤拷锟侥讹拷态匹锟戒！锟斤拷锟斤拷	
			 */
			method.invoke(this, request, response);
			
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	/**
	 * 	锟斤拷锟斤拷锟铰� and 锟斤拷锟斤拷没锟斤拷锟斤拷锟斤拷耄★拷锟斤拷锟�
	 * 
	 * @param request
	 * 
	 * 
	 * @param response
	 */
	private void login(HttpServletRequest request, HttpServletResponse response) {
		try {
			System.out.println(request.getParameter("Name"));
			System.out.println(request.getParameter("Password"));
			if(userDaoJdbcEntity.Login(request.getParameter("Name"), request.getParameter("Password")))
			{
				/**
				 *  锟斤拷录锟缴癸拷锟斤拷锟酵把革拷锟矫伙拷锟斤拷为锟窖碉拷陆锟矫伙拷锟斤拷锟斤拷锟斤拷
				 */
				request.getSession().setAttribute("loginuser", request.getParameter("Name"));
				response.sendRedirect("JSP/CRUD.jsp");
				
			}
			else {
				/**
				 * 	锟斤拷陆失锟杰ｏ拷锟斤拷锟截达拷锟斤拷锟斤拷息锟斤拷锟矫伙拷锟斤拷锟斤拷锟斤拷
				 */
				request.getSession().setAttribute("loginfalse", "锟矫伙拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟�");
				response.sendRedirect("JSP/Login.jsp");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 	锟斤拷锟斤拷注锟斤拷 and 锟斤拷锟斤拷一锟斤拷锟斤拷录锟斤拷锟斤拷锟斤拷
	 * 
	 * @param request
	 * @param response
	 */
	private void register(HttpServletRequest request, HttpServletResponse response) {
		try {
			
			DAO.User user = new DAO.User();
		
			user.setId(Integer.parseInt(request.getParameter("Id")));
			
			user.setName(request.getParameter("Name").toString());
			user.setPassword(request.getParameter("Password").toString());
			
			user.setSex(request.getParameter("Sex").toString());
			
			user.setQuestion(request.getParameter("Question").toString());

			user.setAnswer(request.getParameter("Answer").toString());
			
			user.setEmail(request.getParameter("Email").toString());
			
			long count = userDaoJdbcEntity.countUsers(user.getId());
			
			if (count > 0) {
				
				request.getSession().setAttribute("registeruser", user);
				response.sendRedirect("JSP/Register.jsp");
			} else {
				userDaoJdbcEntity.Register(user);
				request.getSession().setAttribute("register",user.getName());
				response.sendRedirect("JSP/Register.jsp");
				//response.setHeader("refresh", "5;url='JSP/Login.jsp'");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 *	 锟斤拷锟斤拷一锟斤拷锟斤拷录锟斤拷删锟斤拷锟斤拷锟斤拷锟斤拷
	 * 
	 * @param request
	 * @param response
	 */
	private void delete(HttpServletRequest request, HttpServletResponse response) {
		try {
			userDaoJdbcEntity.Delete(Integer.parseInt(request.getParameter("id")));
			response.sendRedirect("query.do");
		} catch (IOException e) {
			e.printStackTrace();
			System.out.println("delete false!");
		}
	}


	/**
	 * 	锟斤拷原锟斤拷息锟斤拷锟叫编辑锟斤拷锟斤拷锟斤拷
	 * 
	 * @param request
	 * @param response
	 */
	private void edit(HttpServletRequest request, HttpServletResponse response) {
		try {
			DAO.User user = userDaoJdbcEntity.CheckUser(Integer.parseInt(request.getParameter("id")));
			request.getSession().setAttribute("upuser", user);
			response.sendRedirect("JSP/CRUD.jsp");
			// request.getRequestDispatcher("JSP/Update.jsp").forward(request,
			// response);转锟斤拷锟结导锟铰讹拷锟斤拷锟斤拷锟斤拷路锟斤拷锟斤拷锟襟！ｏ拷锟斤拷
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 *	 锟斤拷锟斤拷锟斤拷息执锟叫革拷锟铰ｏ拷锟斤拷锟斤拷
	 * 
	 * @param request
	 * @param response
	 */
	private void update(HttpServletRequest request, HttpServletResponse response) {
		try {
			DAO.User user = new DAO.User();
			user.setId(Integer.parseInt(request.getParameter("Id")));
			user.setName(request.getParameter("Name").toString());
			user.setPassword(request.getParameter("Password").toString());
			user.setSex(request.getParameter("Sex").toString());
			user.setQuestion(request.getParameter("Question").toString());
			user.setAnswer(request.getParameter("Answer").toString());
			user.setEmail(request.getParameter("Email").toString());
			if(userDaoJdbcEntity.Update(user)) {response.sendRedirect("query.do");}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 *	 锟斤拷锟斤拷模锟斤拷锟斤拷询 and 锟斤拷锟斤拷锟劫诧拷询锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void query(HttpServletRequest request, HttpServletResponse response) throws IOException {	
		if(request.getParameter("id")==null) {
		Dim dim = new Dim();
		dim.setName(request.getParameter("Name"));
		dim.setPassword(request.getParameter("Password"));
		dim.setSex(request.getParameter("Sex"));
		dim.setQuestion(request.getParameter("Question"));
		dim.setAnswer(request.getParameter("Answer"));
		dim.setEmail(request.getParameter("Email"));
		ArrayList<DAO.User> users = userDaoJdbcEntity.CheckDimUsers(dim);
		request.getSession().setAttribute("dimuser", users);
		response.sendRedirect("JSP/CRUD.jsp");}
		else {
		int Id=Integer.parseInt(request.getParameter("id"));
		DAO.User user=userDaoJdbcEntity.CheckUser(Id);
		request.getSession().setAttribute("privateuser", user);
		//Cookie cookie=new Cookie("viewusers",user.getName());
		//cookie.setMaxAge(10);
		cookie.setValue(user.getName());
		cookie.setPath("/");
		response.addCookie(cookie);	
		response.sendRedirect("JSP/CRUD.jsp");
	}
}
	
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
